<?php
/**
 * 缓存刷新
 *
 * @author suning <snsnsky@gmail.com>
 */

define('SITE_AUTH', 'tool-cacherefresh');
require('../../../common.inc.php');

class cacherefresh extends \lib\core\AdminAction
{
    public function doDefault()
    {
        if (!empty($_POST['url']))
        {
            $url = explode("\n", $_POST['url']);
            $urls = [];
            $flag = false;

            foreach ($url as $v)
            {
                $v = trim($v);
                if (!empty($v))
                {
                    if (filter_var($v, FILTER_VALIDATE_URL))
                    {
                        $urls[] = $v;
                    }
                    else
                    {
                        $flag = true;
                        break;
                    }
                }
            }

            if ($flag)
            {
                self::output(['title' => '提交失败', 'text' => 'url地址存在不合法', 'type' => 'error']);
            }

            cdn_cache_purge($urls);
            $this->adminLog('缓存刷新', 0, 'purge', $urls);
            self::output(['title' => '操作成功', 'type' => 'success']);
        }

        $smarty = $this->app->page();
        $smarty->output('tool/cacherefresh.tpl');
    }
}
$app->run();